<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class admincp extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this->load->helper(array('form', 'url'));
		@session_start();
	}

	public function index($nof =null)
	{
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			//$this->load->model('order_model');
			//$this->load->model('product_model');
			//$data['total_order'] = $this->order_model->get_total_order_inday();
			//$data['total_items'] = $this->product_model->total_product();
			$this->load->view('backend/dashboard'); 
		}
	}
	public function category(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('category_model');
			$config['base_url'] = site_url('admincp/question');
			$config['total_rows'] = $this->category_model->total_category();
			$config['per_page'] = 20;
			$config['prev_link'] = 'Đầu';
			$config['next_link'] = 'Tiếp';
			$this->pagination->initialize($config); 
			$data['listcategory'] = $this->category_model->get_all_category($config['per_page'],$this->uri->segment(3));
			$data['category'] = $this->category_model->listallcategory();
			$data['pages'] = $this->pagination->create_links();
			$this->load->view('backend/dashboard',$data); 
		}
	}
	public function reply_now($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('category_model');
			$this->load->model('question_model'); 
			$this->load->model('answer_model'); 
			$data['id'] = $id;
			$data['notification'] = "";
			if(isset($_REQUEST['bttsubmit'])){
				$content = $this->input->post('content_question', true);
				$this->answer_model->add_answer($id,$content,"Admin","");
				$data['notification'] = "Trả lời thành công !";
				redirect('admincp/reply_now');
			}
			
			$this->load->view('backend/reply_now',$data); 
		}
	}
	public function add_category(){ 
			if($this->session->userdata('adminnam') == null){
				redirect('admincp/login');
			}else{
				if (isset($_REQUEST['submit'])) { 
				$name = $this->input->post('catename', true);
				$type = $this->input->post('catetype', true);
				$root =$this->input->post('cateroot',true);
				
				$this->load->model('category_model');
				$result = $this->category_model->insert_category($name, $type,$root);
				
				if($result == 0){
					redirect('admincp/add_category/');
				}else {
					 redirect('admincp/category/');
				} 
			}
			$this->load->model('category_model');
			$data['category'] = $this->category_model->listallcategory();
			$this->load->view('backend/dashboard',$data);  
		}
	}
	
	public function edit_category($id=null){ 
			if($this->session->userdata('adminnam') == null){
				redirect('admincp/login');
			}else{
				$this->load->model('category_model');
				if (isset($_REQUEST['submit'])) { 
				$name = $this->input->post('catename', true);
				$type = $this->input->post('catetype', true);
				$root =$this->input->post('cateroot',true);  
				$result = $this->category_model->update_category($id,$name, $type,$root); 
				if($result == 0){
					redirect('admincp/edit_category/'.$id);
				}else {
					 redirect('admincp/category/');
				} 
			} 
			$data['category'] = $this->category_model->listallcategory();
			if($id != null){
			$data['details'] = $this->category_model->get_category_details($id);
			}else{
				 redirect('admincp/category/');
			}
			$this->load->view('backend/dashboard',$data);  
		}
	}
	
	public function del_cate($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('category_model');
			$this->category_model->delete_category($id);
			redirect('admincp/category');
		}
	}
	
	public function question(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('question_model'); 
			$config['base_url'] = site_url('admincp/question');
			$config['total_rows'] = $this->question_model->total_question();
			$config['per_page'] = 20;
			$config['prev_link'] = 'Đầu';
			$config['next_link'] = 'Tiếp';
			$this->pagination->initialize($config); 
			$data['list_question'] = $this->question_model->get_all_question($config['per_page'],$this->uri->segment(3));
			$this->load->model('category_model');
			$data['category'] = $this->category_model->listallcategory();
			$data['pages'] = $this->pagination->create_links();
			$this->load->view('backend/dashboard',$data); 
		}
	}
	
	public function updatestatus_question($id){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			 $this->load->model('question_model');
			 $this->question_model->update_question_status($id);
			redirect('admincp/question');
		}
	}
	
	public function del_question($id=null){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			 $this->load->model('question_model');
			 $this->question_model->delete_question($id);
			redirect('admincp/question');
		}
	}
	
	public function answer(){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('answer_model'); 
			$this->load->model('question_model'); 
			$config['base_url'] = site_url('admincp/answer');
			$config['total_rows'] = $this->answer_model->total_answer();
			$config['per_page'] = 20;
			$config['prev_link'] = 'Đầu';
			$config['next_link'] = 'Tiếp';
			$this->pagination->initialize($config); 
			$data['listanswer'] = $this->answer_model->get_all_answer($config['per_page'],$this->uri->segment(3));
			$data['pages'] = $this->pagination->create_links();
			$data['questionid'] = $this->question_model->list_all_question();
			$this->load->view('backend/dashboard',$data); 
		}
	}
	
	public function update_answer($id){
		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('answer_model');
			$this->answer_model->update_answer_status($id);
			redirect('admincp/answer');
		}
	}
	
	public function del_answer($id){
	if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			$this->load->model('answer_model');
			$this->answer_model->delete_answer($id);
			redirect('admincp/answer');
		}
	}

	public function login(){
		if (isset($_REQUEST['commitlogin'])) {
			$name = $this->input->post('uname', true);
			$pass = $this->input->post('upass', true);
			$this->load->model('user_model');
			$result = $this->user_model->checkuser($name, $pass);
			if($result == 0){
				redirect('admincp/login/');
			}else {
				$newdata = array(
						'adminid'  => $result,
						'adminnam'     => $name
				);
				$this->session->set_userdata($newdata);
				redirect('admincp/index');
			}
		}
			
		$this->load->view('backend/login');
	}




	public function fmenu(){

		if($this->session->userdata('adminnam') == null){
			redirect('admincp/login');
		}else{
			  
			if (isset($_REQUEST['submit'])) {
				$menuname   = $this->input->post('menuname', true);
				$menutext   = $this->input->post('menutext', true); 
				$this->menu_model->_addmenu($menuroot,$menuname,$menutext,$menuorder,$menuactive);
				redirect('admincp/fmenu');
			} 
			$this->load->view('appstore/admin/dashboard',$data);
		}
	}


	function do_upload_image($mypath,$filename){
		$config['upload_path'] = $mypath;
		$config['allowed_types'] = 'gif|jpg|png|bmp';
		$config['max_size']	= '80000';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if(isset($filename))
		{
			//echo $filename; die();
			if (!$this->upload->do_upload($filename))
			{
				$error = array('error' => $this->upload->display_errors());
				return NULL;
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$imagename = $this->upload->file_name;
				return $imagename;
			}
		}else {
			echo $this->upload->display_errors();
		}
	}


	function removesign($str)
	{
		$coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
		,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ","ê","ù","à");
				$khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D","e","u","a");
				return str_replace($coDau,$khongDau,$str);
	}

	public function logout(){
		$this->session->unset_userdata('adminid');
		$this->session->unset_userdata('adminnam');
		$this->session->sess_destroy();
		redirect('admincp');
	}

}
